Page({
  data: {
    isHeaderShowing:true,     //是否显示功能栏
    typeList:[                //分类列表
      '所有',
      '画作',
      '明信片',
      '花瓶'
    ],
    typeIndex:0,          //分类索引
    pageNo:0,             //页数
    isEnd:false,          //是否到底
    goodsList:[]          //商品列表
  },
  //bindChangeAlterType 分类切换响应函数
  bindChangeAlterType(e){
    this.setData({
      typeIndex:+e.detail.value,
      goodsList:[],
      pageNo:0,
      isEnd:false
    });
    this.clearKeyword();
    this.getGoodsList();
  },
  //bindTapNavToDetail 点击跳转商品详情页响应函数
  bindTapNavToDetail(e){
    if(e.target.dataset.gid == undefined) return;
    var gid = +e.target.dataset.gid;
    if(gid == NaN) return;
    var goodData = this.data.goodsList.filter((item)=>item.gid == gid);
    wx.navigateTo({
      url: '/pages/goodDetail/goodDetail',
      success: function(res) {
        res.eventChannel.emit('goodData', { data: goodData })
      }
    });
  },
  //forScroll 滚动响应函数
  forScroll(e){
    //滚动判断显示功能栏
    if(e.detail.deltaY<0){
      this.setData({
        isHeaderShowing:false
      })
    }else{
      this.setData({
        isHeaderShowing:true
      })
    }
  },
  //bindTouchBottomGoodsAdd 触底刷新响应函数
  bindTouchBottomGoodsAdd(){
    //触底刷新
    if(this.data.isEnd==true) return;
    this.setData({
      pageNo:this.data.pageNo++
    });
    this.getGoodsList();
  },
  //getGoodsList 获取商品函数
  getGoodsList(){
    if(this.data.isEnd == true) return;
    var that = this;
    var app = getApp();
    var keyword = app.globalData.searchKeyword;
    this.setData({pageNo:this.data.pageNo+=1});
    if(keyword!=undefined){
      //搜索查询
      app.pinia('searchGoods',{
        keyword:keyword,
        page:that.data.pageNo,
        pageLength:5
      },(res)=>{
        console.log(res.data);
        var goodsList = res.data;
        //结果更新
        if(goodsList.length==0) this.setData({isEnd:true});
        that.setData({
          goodsList:this.data.goodsList.concat(goodsList)
        });
      });
    }else{
      //分类查询  
      app.pinia('checkGoods',{
        pageNo:that.data.pageNo,
        typeIndex:that.data.typeIndex
      },(res)=>{
        console.log(res.data);
        var goodsList = res.data;
        //结果更新
        if(goodsList.length==0) this.setData({isEnd:true});
        that.setData({
          goodsList:this.data.goodsList.concat(goodsList)
        });
      });
    }
  },
  //clearKeyword 清空关键词函数
  clearKeyword(){
    //这个函数会清空全局数据的searchKeyword，恢复商品列表页的分类搜索状态
    //当页面退出时会调用这个函数
    //当切换为分类查询模式的时候也会调用这个函数
    if((getApp()).globalData.searchKeyword!=undefined) (getApp()).globalData.searchKeyword=undefined;//将关键词清空，恢复分类查询
  },
  //dealPreTypeIndex 预设分类函数
  dealPreTypeIndex(){
    let preTypeIndex = (getApp()).globalData.preTypeIndex;
    if(preTypeIndex != undefined){
      this.setData({typeIndex:preTypeIndex});
    }
  },
  //clearPreTypeIndex 清空预设分类值
  clearPreTypeIndex(){
    if((getApp()).globalData.preTypeIndex!=undefined) (getApp()).globalData.preTypeIndex = undefined;
  },
  onLoad(options){
    this.setData({themeIndex:(getApp()).globalData.themeIndex});    //设置主题
    this.setData({shopcartNum:(getApp()).globalData.shopcart.length});      //获取购物车数量
    this.dealPreTypeIndex();    //预设分类
    this.getGoodsList();    //获取商品数据
  },
  //商品列表页会有两种退出方式
  //一种是返回，一种是通过点击链接跳转到其他页面去
  //那么就会触发以下生命周期函数
  onHide: function() {
    // 页面隐藏/切入后台时触发
    this.clearKeyword();
    this.clearPreTypeIndex();
  },
  onUnload: function() {
    // 页面卸载时触发
    this.clearKeyword();
    this.clearPreTypeIndex();
  }  
})